var wxSortPickerView = require('../../../wxSortPickerView/wxSortPickerView.js');
var constract = require('../../../utils/constract.js');
var app = getApp();
Page({
  data:{
    
  },
  onLoad: function (options) {
    var that = this;
    var state = options.type;
    // var sort = options.sort;
    var sort = 2;
    that.setData({
      state: state,
      sort: sort
    })
  },
  onShow: function () {
    this.getData();
  },
  getData: function(){
    var that = this;

    app.Verification(function (token) {
      wx.request({
        url: constract.findCollect,
        header: { token: token },
        data: { type: that.data.sort, keyword: '' },
        success: function (res) {
          var data = res.data.data;
          that.setData({
            list: data,
            check: false
          });
          wxSortPickerView.init(data, that);
        }
      })
    })
  },
  formSubmit: function (e) {
    var that = this;
    var id = e.detail.value.ids
    var collectIds = [];
    var cardIds = [];
    var ouids = [];

    for (var i = 0; i < id.length; i++){
      var item = id[i];
      var itemArr = item.split(",")
      if (itemArr[1] == 0){
        cardIds.push(itemArr[0])
      } 
      if (itemArr[1] == 1){
        collectIds.push(itemArr[0]);
        ouids.push(itemArr[2]);
      }
    };
    app.globalData.ids = {
      cardIds: cardIds,
      collectIds: collectIds,
      ouids: ouids
    }
    if (that.data.state == 2){
      app.Verification(function (token) {
        wx.request({
          url: constract.batchDelete,
          method: 'POST',
          data: { cardId: app.globalData.ids.cardIds, collectId: app.globalData.ids.collectIds,collection_uid:app.globalData.ids.ouids},
          header:{
            "content-type": "application/x-www-form-urlencoded",
            token: token
          },
          success: function (res) {
            console.log(res);
            if (res.data.code == 12000){
              wx.showToast({
                title: '删除成功',
                mask: true,
                icon: 'success',
                duration: 2000
              })
              that.getData();
              setTimeout(function () {
                wx.switchTab({
                  url: '/pages/card/card'
                })
              }, 1000)
            };
          }
        })
      })
    }else{
      wx.navigateTo({
        url: '/pages/details/group/group'
      });
    }
  },
});